app.controller('evaluate-more', function ($scope, $http) {

    var token = localStorage.getItem('token');
    var interactionId = sessionStorage.getItem('interactionId');
    $scope.page = '';
    //存放回复列表
    $scope.interactions = [];
    //展示一条评论下方所有回复
    $scope.showReview = function (page) {

        $scope.page = page ? page : 1;

        // 若该加载为初始化加载，即没有传入page参数
        if (!page) {
            $scope.page = 1;
        }
        $http({
            method: 'GET',
            url: app.netPath + '/interaction/show/interactions',
            headers: {Authorization: token},
            params: {
                'interactionId': interactionId,
                'page': $scope.page
            }
        }).then(function (res) {
            console.log(res.data);
            var infiniteScroll = $('.infinite-scroll');
            var infiniteScrollPreloader = $('.infinite-scroll-preloader');
            if (!res.data.error) {
                //没有回复的情况
                if (res.data.data.dynamicId) {
                    $scope.interactioninfo = [];
                    $scope.interactioninfo.user = res.data.data.user;
                    $scope.interactioninfo.interactionDate = res.data.data.interactionDate;
                    $scope.interactioninfo.dynamicComment = res.data.data.dynamicComment;
                    $scope.interactioninfo.interactionId = res.data.data.interactionId;
                    $scope.interactioninfo.dynamicId = res.data.data.dynamicId;

                    //判断用户头像是否存在
                    if (res.data.data.user.userAvatarAddress) {
                        $scope.interactioninfo.user.userAvatarAddress = app.netPath + '/resources/img/' + $scope.interactioninfo.user.userAvatarAddress;

                    }
                } else {
                    //有回复的情况
                    $scope.interactioninfo = [];
                    $scope.interactioninfo.user = res.data.data[0].user;
                    $scope.interactioninfo.interactionDate = res.data.data[0].interaction2.interactionDate;
                    $scope.interactioninfo.dynamicComment = res.data.data[0].interaction2.dynamicComment;
                    $scope.interactioninfo.interactionId = res.data.data[0].interaction2.interactionId;
                    $scope.interactioninfo.dynamicId = res.data.data[0].interaction2.dynamicId;
                    //判断动态发表用户头像是否存在
                    if (res.data.data[0].user.userAvatarAddress) {
                        $scope.interactioninfo.user.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[0].user.userAvatarAddress;
                        app.buildImageUrl($scope.interactioninfo.user, 'userAvatarAddress', function () {
                            $scope.$digest();
                        });
                    }

                    //回复者头像
                    for (var i = 0; i < res.data.data.length; i++) {
                        if (res.data.data[i].user1.userAvatarAddress) {
                            res.data.data[i].user1.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].user1.userAvatarAddress;
                            app.buildImageUrl(res.data.data[i].user1, 'userAvatarAddress', function () {
                                $scope.$digest();
                            });
                        }
                        $scope.interactions.push(res.data.data[i]);
                    }
                }

                $scope.isOnloading = false;

                // 启用无限加载事件
                $.attachInfiniteScroll(infiniteScroll);
                $scope.thispage = $scope.interactions.length % 15 == 0 ? $scope.interactions.length / 15 : $scope.interactions.length / 15 + 1;
                $scope.thispage=parseInt($scope.thispage);
                console.log($scope.thispage);
                if (res.data.allPage <= $scope.thispage) {
                    // 加载完毕，则注销无限加载事件，以防不必要的加载
                    $.detachInfiniteScroll(infiniteScroll);
                    // 删除加载提示符
                    infiniteScrollPreloader.hide();
                }

            }

            if (res.data.error) {
                $.toast(res.data.msg)
            }

        },function(){
            $.toast('网络走丢了！')
        })

    };
    //添加评论回复
    $scope.addReview = function () {
        //判断有无证书
        if (!token) {
            $.toast('请先登录');
            location.href = '#user-login';
            return;
        }
        if (token) {
            //定义对象
            $scope.interaction = {
                "parentId": $scope.interactioninfo.interactionId,
                'dynamicComment': $scope.interaction.dynamicComment,
                'interactionImg': "",
                'replayId': $scope.interactioninfo.user.userId,
                'dynamicId': $scope.interactioninfo.dynamicId
            };
            if ($scope.interaction.dynamicComment != null && $scope.interaction.dynamicComment != "") {
                $http({
                    method: 'POST',
                    url: app.netPath + '/interaction/add/Reply',
                    headers: {Authorization: token},
                    data: $scope.interaction
                }).then(function (res) {
                    console.log(res.data);
                    if (!res.data.error) {
                        $.toast('评论成功！');
                        location.href = '#evaluate-more'
                    }
                    else {
                        $.toast(res.data.msg)
                    }
                })
            }

        }
    };
    //定义对象
    $scope.interaction = {};
    //  获取对应提交数据
    $scope.getUpData = function (nickName, parentId, replayId, dynamicId) {
        $("#bottom1").css("display", "none");
        $("#bottom2").css("display", "block");
        $scope.userNickName = nickName;
        // alert(nickName);
        // alert(parentId);
        // alert(replayId);
        // alert(dynamicId);
        //对象赋值
        $scope.interaction = {
            "parentId": parentId,
            'dynamicComment': $scope.interaction.dynamicComment,
            'interactionImg': "",
            'replayId': replayId,
            'dynamicId': dynamicId
        };

    };
    //添加对层主的回复
    $scope.addOtherReview = function () {
        if ($scope.interaction.dynamicComment != null && $scope.interaction.dynamicComment != "") {
            $http({
                method: 'POST',
                url: app.netPath + '/interaction/add/Reply',
                headers: {Authorization: token},
                data: $scope.interaction
            }).then(function (res) {
                console.log(res.data);
                if (!res.data.error) {
                    $.toast('评论成功！');
                    location.href = '#evaluate-more'

                }
                else {
                    $.toast(res.data.msg)
                }
            })
        }
    };

    //点击楼主重新获取回复列表
    $scope.reviewMore = function (dynamicId, userId) {

        sessionStorage.setItem('dynamicId', dynamicId);
        sessionStorage.setItem('userId', userId);
        location.href = '#evaluate-more'
    };
    $scope.getDetail=function(){
        sessionStorage.setItem('dynamicId', $scope.interactioninfo.dynamicId);
        location.href = '#evaluate-details';
    }

});